package org.serverct.finalfight.game.hadesgame.events;

import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import org.serverct.finalfight.FinalFight;
import org.serverct.finalfight.game.hadesgame.EventType;
import org.serverct.finalfight.internal.game.basic.BasicGame;
import org.serverct.finalfight.internal.game.hadesgame.ContinuousEvent;

import java.util.HashSet;
import java.util.UUID;

public class Vegetarian extends ContinuousEvent implements Listener {

    public Vegetarian(BasicGame game, EventType type) {
        super(game, type);
        vegetables.add(Material.APPLE);
        vegetables.add(Material.MUSHROOM_STEW);
        vegetables.add(Material.BREAD);
        vegetables.add(Material.GOLDEN_APPLE);
        vegetables.add(Material.CAKE);
        vegetables.add(Material.COOKIE);
        vegetables.add(Material.MELON);
        vegetables.add(Material.CARROT);
        vegetables.add(Material.POTATO);
        vegetables.add(Material.BAKED_POTATO);
        vegetables.add(Material.POISONOUS_POTATO);
        vegetables.add(Material.PUMPKIN_PIE);
        vegetables.add(Material.BEETROOT);
        vegetables.add(Material.BEETROOT_SOUP);
        vegetables.add(Material.GOLDEN_CARROT);
    }

    public static HashSet<Material> vegetables = new HashSet<>();
    public static HashSet<UUID> eaters = new HashSet<>();

    @EventHandler
    public void onConsume(PlayerItemConsumeEvent event) {
        ItemStack item = event.getItem();
        Player player = event.getPlayer();
        if (!this.game.players.containsKey(player.getUniqueId())) {
            return;
        }
        if (!this.game.isAlive(player)) {
            return;
        }
        if (item.getType().isEdible()) {
            if (!vegetables.contains(item.getType())) {
                eaters.add(player.getUniqueId());
                new BukkitRunnable() {
                    @Override
                    public void run() {
                        eaters.remove(player.getUniqueId());
                    }
                }.runTaskLaterAsynchronously(FinalFight.plugin, 1L);
            }
        }
    }

    @EventHandler
    public void onFoodLevelChange(FoodLevelChangeEvent event) {
        Player player = (Player) event.getEntity();
        if (eaters.contains(player.getUniqueId())) {
            int tmp = event.getFoodLevel() - player.getFoodLevel();
            event.setFoodLevel(player.getFoodLevel() - tmp);
        }
    }
}
